package action;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.UserManager;
import service.impl.UserManagerImpl;
import domain.User;

public class AddUserAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward af = null; 
		User userform = (User) form;
		UserManager um = new UserManagerImpl();
		// System.out.println("----" + userform.getId());		
		String action=request.getParameter("action");
		System.out.println(action);
		if(action.equals("editpre")){
			String id_str=request.getParameter("id");
			System.out.println(action);
			int id=Integer.parseInt(id_str);
			request.setAttribute("user", um.findById(id));
			af=new ActionForward("/user/EditUser.jsp");
		}else if(action.equals("add")){
			if (um.register(userform)) {
				af=new ActionForward("/listUser.do");
			} else {		
				af=new ActionForward("/user/AddUser.jsp");
			}
		}else if(action.equals("edit")){
			if (um.edit(userform)) {
				af=new ActionForward("/listUser.do");
			} else {
				af=new ActionForward("/listUser.do");
			}
		}
		request.setAttribute("msg", um.getMsg());
		return af;
	}

}
